package com.fox.hiring.common.util;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fox.hiring.company.entity.CompanyEntity;
import com.fox.hiring.talent.entity.TalentEntity;

/**
 * 系统工具类
 * 
 * @author fox.
 * @since 2016年3月2日
 */

public class AppUtil {

	private AppUtil() {

	}

	/**
	 * 判断求职者是否登录
	 * 
	 * @param request
	 * @return
	 */
	public static boolean isLogin(HttpServletRequest request) {
		Object obj = request.getSession().getAttribute("talent");
		return obj == null ? false : true;
	}

	/**
	 * 获取session里的talentId
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 */
	public static int getTalentId(HttpServletRequest request,
			HttpServletResponse response) throws ServletException {
		Object obj = request.getSession().getAttribute("talent");
		if (obj == null) {
			try {
				request.getRequestDispatcher(
						"view/talent/login.jsp?sessionInvalidate=true")
						.forward(request, response);
			} catch (IOException e) {
				e.printStackTrace();
			}
			throw new IllegalStateException("会话已关闭！");
		} else {
			TalentEntity talent = (TalentEntity) obj;
			return talent.getId();
		}
	}
	/**
	 * 获取session里面的companyId
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 */

	public static int getCompanyId(HttpServletRequest request,
			HttpServletResponse response) throws ServletException {
		Object obj = request.getSession().getAttribute("company");
		if (obj == null) {
			try {
				request.getRequestDispatcher(
						"view/company/login.jsp?sessionInvalidate=true")
						.forward(request, response);
			} catch (IOException e) {
				e.printStackTrace();
			}
			throw new IllegalStateException("会话已关闭！");
		} else {
			CompanyEntity company = (CompanyEntity) obj;
			return company.getId();
		}
	}
}